
import Koa from 'koa';
import Router from './router/router';
import Bodyparser from 'koa-bodyparser';
import Cors from 'koa2-cors';
import Middleware from './middleware/index';
import { openUrl } from './config/index';
import { connect, connection as db } from 'mongoose';

//mongoDB连接
connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });
db.on('errpr', () => { console.log('\x1B[31m%s\x1B[39m', 'mongoDB连接失败'); });
db.once('open', () => { console.log('\x1B[32m%s\x1B[39m', 'mongoDB连接成功'); });

const app = new Koa();
/**解决跨域 */
app.keys = ['some secret hurr'];
app.use(Cors());
/**token验证 */
app.use(Middleware.verifyToken(openUrl));
/**错误中间件 */
app.use(Middleware.errorHandler);
/**解析request.body */
app.use(Bodyparser());
/**注册router */
app.use(Router.routes());
app.listen(3000);
console.log('\x1B[36m%s\x1B[0m', 'http://localhost:3000/');
